<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Comments
 * @description : Process photo, video, poll comments
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Comment extends AUTH_Controller {

  /**
   * Post Comment Via AJAX
   * 
   */
  public function post() {

    // Checking request type
    if ($this->is_post() && $this->is_ajax()) {

      // Preparing response array
      $response_array = array('status' => 0, 'msg' => '', 'data' => '');

      // Comment type must be one of this
      $comment_types = array('album', 'photo', 'poll', 'video');

      // Getting inputs
      $comment_type = $this->input->post('comment_type', TRUE);
      $comment_type_id = $this->input->post('comment_type_id', TRUE);
      $comment_message = $this->sanitize_html($this->input->post('comment_message', TRUE));

      // Validating Inputs
      if (!empty($comment_type) && in_array($comment_type, $comment_types) && !empty($comment_type_id) && is_numeric($comment_type_id) && !empty($comment_message)) {

        // Checking the source
        if ($comment_type == 'photo' || $comment_type == 'video') {
          $comment_type_data = $this->album_model->get_photo_by_id($comment_type_id);
        } else if ($comment_type == 'poll') {
          $comment_type_data = $this->poll_model->get_poll_by_id($comment_type_id, NULL);
        } else {
          $comment_type_data = array('status' => '', 'result' => '');
        }

        // Posting comment
        if ($comment_type_data['status'] == 1 && !empty($comment_type_data['result'])) {
          $add_comment = $this->comment_model->add_comment($comment_type, $comment_type_id, $this->current_user['user_id'], $comment_message);
          if ($add_comment['status'] == 1 && !empty($add_comment['result'])) {
            $response_array['status'] = 1;

            // Set mentioning notificaion
            $mentioned_users = get_mentioned_users($comment_message);
            if (!empty($mentioned_users)) {
              foreach ($mentioned_users as $mentioned_user) {
                if ($mentioned_user['user_id'] != $this->current_user['user_id']) {
                  $post_id = make_post_id('comment', $add_comment['result']);
                  $toast_text = '@' . $this->current_user['user_name'] . ' mentioned you in a comment <b>#' . $post_id . '</b>';
                  @add_toast_notificaion($mentioned_user['user_id'], $toast_text, $post_id);
                }
              }
            }
          }
        }
      }

      // Responding json
      die(json_encode($response_array));
    }

    // Redirect if invalid request
    redirect();
  }

  /**
   * Delete comment via AJAX
   * 
   */
  public function delete($comment_id = NULL) {

    // Checking request type
    if ($this->is_post() && $this->is_ajax()) {

      // Preparing response array
      $response_array = array('status' => 0, 'msg' => '', 'data' => '');

      if (!empty($comment_id) && is_numeric($comment_id)) {

        // Deleting comment
        if ($this->is_admin) {
          $del_comment = $this->comment_model->del_comment_by_id($comment_id);
          if ($del_comment['status'] == 1 && !empty($del_comment['result'])) {
            $this->like_model->del_likes_by_post('comment', $comment_id);
            $response_array['status'] = 1;
          }
        } else {
          $del_comment = $this->comment_model->del_comment($comment_id, $this->current_user['user_id']);
          if ($del_comment['status'] == 1 && !empty($del_comment['result'])) {
            $this->like_model->del_likes_by_post('comment', $comment_id);
            $response_array['status'] = 1;
          }
        }
      }

      // Responding json
      die(json_encode($response_array));
    }

    // Redirect if invalid request
    redirect();
  }

  /**
   * Load new comments via ajax
   * 
   */
  public function load_new_comments() {

    // Checking request type
    if ($this->is_post() && $this->is_ajax()) {

      // Preparing response array
      $response_array = array('status' => 0, 'new_last_comment_id' => '', 'data' => '', 'deleted_comment_ids' => '');

      // Init variables
      $deleted_comment_ids = array();
      $comment_data = array();
      $comment_types = array('album', 'photo', 'video', 'poll');

      // Getting values
      $comment_type = $this->sanitize($this->input->post('comment_type', TRUE));
      $comment_type_id = $this->sanitize($this->input->post('comment_type_id', TRUE));
      $comment_last_id = $this->sanitize($this->input->post('comment_last_id', TRUE));

      // Getting total comments count
      $total_comments_count = 0;
      $get_comments_count = $this->comment_model->get_comments($comment_type, $comment_type_id, NULL);
      if ($get_comments_count['status'] == 1 && !empty($get_comments_count['result'])) {
        $total_comments_count = $get_comments_count['count'];
      }

      // Validating source
      if (!empty($comment_type) && in_array($comment_type, $comment_types) && !empty($comment_type_id) && is_numeric($comment_type_id) && is_numeric($comment_last_id)) {

        // Getting source data
        if ($comment_type == 'photo' || $comment_type == 'video') {
          $comment_type_data = $this->album_model->get_photo_by_id($comment_type_id);
        } else if ($comment_type == 'poll') {
          $comment_type_data = $this->poll_model->get_poll_by_id($comment_type_id, NULL);
        } else {
          $comment_type_data = array('status' => '', 'result' => '');
        }

        if ($comment_type_data['status'] == 1 && !empty($comment_type_data['result'])) {

          // Getting & preparing new comments
          $get_new_comments = $this->comment_model->get_new_comments($comment_type, $comment_type_id, $comment_last_id);
          if ($get_new_comments['status'] == 1 && !empty($get_new_comments['result'])) {
            $new_comments = $get_new_comments['result'];
            $view_data = array();
            $view_data['comment_type_data'] = $comment_type_data['result'];
            foreach ($new_comments as $comment) {
              $html_data = array();
              $view_data['comment'] = $comment;
              $view_data['comment_type'] = $comment_type;
              $view_data['comment_type_id'] = $comment_type_id;
              $html_data['comment_html'] = trim($this->load->view('comment_new_ajax', $view_data, TRUE));
              $html_data['comment_id'] = $comment['comment_id'];
              $comment_data[] = $html_data;
            }
          }

          // Getting delted comment ids
          $get_deleted_comments = $this->comment_model->get_deleted_comments($comment_type, $comment_type_id, NULL);
          if ($get_deleted_comments['status'] == 1 && !empty($get_deleted_comments['result'])) {
            foreach ($get_deleted_comments['result'] as $deleted_comment) {
              $deleted_comment_ids[] = $deleted_comment['comment_id'];
            }
          }
        }
      }

      // Setting last newly loaded comment id
      if (isset($new_comments[0]['comment_id'])) {
        $new_last_comment_id = $new_comments[0]['comment_id'];
      } else {
        $new_last_comment_id = '';
      }

      // Preparing response array
      $response_array = array('status' => 1, 'comment_last_id_new' => $new_last_comment_id, 'data' => $comment_data, 'deleted_comment_ids' => $deleted_comment_ids, 'total_comments_count' => $total_comments_count);

      // Respoding json
      die(json_encode($response_array));
    }

    // Redirect if invalid request
    redirect();
  }

  /**
   * Load old comments via ajax
   * 
   */
  public function load_old_comments() {

    // Checking request type
    if ($this->is_post() && $this->is_ajax()) {

      // Preparing response array
      $response_array = array('status' => 0, 'new_last_loaded_comment_id' => '', 'data' => '');

      // Init variables
      $comment_data = array();
      $comment_types = array('album', 'photo', 'video', 'poll');

      // Getting inputs
      $comment_type = $this->sanitize($this->input->post('comment_type', TRUE));
      $comment_type_id = $this->sanitize($this->input->post('comment_type_id', TRUE));
      $comment_last_loaded_id = $this->sanitize($this->input->post('comment_last_loaded_id', TRUE));

      // Checking source type
      if (!empty($comment_type) && in_array($comment_type, $comment_types) && !empty($comment_type_id) && is_numeric($comment_type_id) && is_numeric($comment_last_loaded_id)) {

        // Getting source data
        if ($comment_type == 'photo' || $comment_type == 'video') {
          $comment_type_data = $this->album_model->get_photo_by_id($comment_type_id);
        } else if ($comment_type == 'poll') {
          $comment_type_data = $this->poll_model->get_poll_by_id($comment_type_id, NULL);
        } else {
          $comment_type_data = array('status' => '', 'result' => '');
        }

        // Checking data
        if ($comment_type_data['status'] == 1 && !empty($comment_type_data['result'])) {

          // Getting & preparing old comments
          $get_old_comments = $this->comment_model->get_old_comments($comment_type, $comment_type_id, $comment_last_loaded_id);
          if ($get_old_comments['status'] == 1 && !empty($get_old_comments['result'])) {
            $old_comments = $get_old_comments['result'];
            $view_data = array();
            $view_data['comment_type_data'] = $comment_type_data['result'];
            foreach ($old_comments as $comment) {
              $html_data = array();
              $view_data['comment'] = $comment;
              $view_data['comment_type'] = $comment_type;
              $view_data['comment_type_id'] = $comment_type_id;
              $html_data['comment_html'] = trim($this->load->view('comment_new_ajax', $view_data, TRUE));
              $html_data['comment_id'] = $comment['comment_id'];
              $comment_data[] = $html_data;
            }
          }
        }
      }

      // Setting last loaded comment id in the end
      if (!empty($old_comments)) {
        $comment_array_end = end($old_comments);
        $new_last_loaded_comment_id = $comment_array_end['comment_id'];
      } else {
        $new_last_loaded_comment_id = '';
      }

      // Checking weather any more comments
      $has_more = 0;
      if (!empty($new_last_loaded_comment_id)) {
        $get_old_comments = $this->comment_model->get_old_comments($comment_type, $comment_type_id, $new_last_loaded_comment_id);
        if ($get_old_comments['status'] == 1 && !empty($get_old_comments['result'])) {
          $has_more = 1;
        }
      }

      // Preparing response
      $response_array = array('status' => 1, 'new_last_loaded_comment_id' => $new_last_loaded_comment_id, 'data' => $comment_data, 'has_more' => $has_more);

      // Responding json
      die(json_encode($response_array));
    }

    // Redirect if invalid request
    redirect();
  }

}

?>